from django.db import models
from django.contrib.auth.models import User
from django.contrib.syndication.feeds import Feed
from django.core import validators 
from django import forms 
#from arrrt.middleware import threadlocals


class Course(models.Model):
    name = models.CharField(maxlength=30, unique=True) 
    class Meta:
	verbose_name = "Class"
	verbose_name_plural = "Classes"
    class Admin:
        pass
        
    def __str__(self):
        return self.name
        
   
#class models.CharField(maxlength=4)
#    classname = models.ForeignKey(Class)
#    
#    class Meta:
#	unique_together = (("groupname", "classname"),)
#
#    class Admin:
#        list_display = ("groupname", "classname")
#    
#    def __str__(self):
#        return '%s %s' % (self.groupname, self.classname)


class Homework(models.Model):
    course = models.ForeignKey(Course)
    id = models.AutoField(primary_key=True)
    duedate = models.DateField()
    work = models.TextField()
    summary = models.TextField(maxlength=30)
    class Admin:
        list_display = ("course", "duedate", "work", "summary")

    def __str__(self):
	return self.work

    def get_absolute_url(self):
        return "/hwk/detail/%s" % self.id



class HomeworkUpload(models.Model):
    hwkfile = models.FileField(upload_to='homeworkbin')
    homeworkid = models.ForeignKey(Homework)
    mark = models.CharField(maxlength=3, blank=True)
    owner = models.ForeignKey(User)
    uptime = models.DateTimeField()
    class Admin:
        list_display = ("uptime", "hwkfile", "homeworkid")

    def __str__(self):
	return self.hwkfile
	

class NewsItem(models.Model):
    title = models.CharField(maxlength=50)
    id = models.AutoField('ID', primary_key=True)
    author = models.ForeignKey(User)
    news = models.TextField()
    date = models.DateField()
    class Admin:
	list_display = ("title", "author", "news")


class UserProfile(models.Model):
        courses = models.ManyToManyField(Course)
        USER_CHOICES = (
           ('TE', 'Teacher'),
           ('ST', 'Student'),
           ('AD', 'Admin'),
        )
        permissions = models.CharField(maxlength=2, choices=USER_CHOICES)
        user = models.OneToOneField(User)

        class Admin:
           pass

	def __str__(self):
	   return '%s' % (self.user)


#   def save(self, new_data):
#        u = UserProfile(new_data['new_user'],
#                        new_data[''])


#        u.first_name = new_data['first_name']
#        u.last_name = new_data['last_name']
#       u.isActive = True
#       u.save()
#       return u


class RegistrationForm(forms.Manipulator):

   def __init__(self):
	self.fields = (
	   	forms.TextField(field_name='username',
				length=30, maxlength=30,
				is_required=True, validator_list=[validators.isAlphaNumeric,
								self.isValidUsername]),
		forms.PasswordField(field_name='password1',
				    length=30, maxlength=30,
				    is_required=True),
		
		forms.PasswordField(field_name='password2',
				    length=30, maxlength=30,
				    is_required=True,
				    validator_list=[validators.AlwaysMatchesOtherField('password1',
											'Passwords must match')]),
		forms.TextField(field_name='first_name',
				length=30, maxlength=30,
				is_required=True),

		forms.TextField(field_name='last_name', 
				length=30, maxlength=30,
				is_required=True),

		forms.EmailField(field_name='email', 
				length=30, 
				maxlength=30, 
				is_required=True),

#		forms.CheckboxSelectMultipleField(field_name='courses', 
#					  choices=list(Class.objects.all())),
#
#		forms.SelectMultipleField(field_name='permissions', 
#					  choices=(USER_CHOICES)),

		)


   def isValidUsername(self, field_data, all_data): 
	try:
	   User.objects.get(username=field_data)
	except User.DoesNotExist:
	   return
	raise validators.ValidationError('The usernme "%s" is already taken.' % field_data)

   def save(self, new_data):
	u = User.objects.create_user(new_data['username'],
				     new_data['email'],
				     new_data['password1'])


	u.first_name = new_data['first_name']
	u.last_name = new_data['last_name']
	u.isActive = True
	u.save()
	return u
